package com.example.layui_project.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.layui_project.comm.action.BaseController;
import com.example.layui_project.dto.JsonData;
import com.example.layui_project.dto.TableParams;
import com.example.layui_project.dto.TableResult;
import com.example.layui_project.entity.SysPermission;
import com.example.layui_project.entity.SysUser;
import com.example.layui_project.enums.ResultCode;
import com.example.layui_project.service.PermissionService;
import com.example.layui_project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("permission")
public class PermissionController extends BaseController {
    @Autowired
    private PermissionService permissionService;
    @GetMapping("toPermissionList.do")
    public String toPermissionList(){
        boolean permission = super.checkPermission();
        if (!permission){
            // 说明权限校验未通过返回
            return "error";
        }
        return "permission/permissionList";
    }

    @RequestMapping("findData.do")
    @ResponseBody
    public TableResult findData(int page, int limit,String title){
        TableParams tableParams = new TableParams();
        HashMap<String, Object> ps = new HashMap<>();
        ps.put("title",title);
        tableParams.setPs(ps);
        tableParams.setPageNum(page-1);
        tableParams.setPageSize(limit);
        TableResult<SysPermission> sysUserTableResult = permissionService.find(tableParams);
        sysUserTableResult.setCode(0);
        return sysUserTableResult;
    }

    @PostMapping("add.do")
    @ResponseBody
    public JsonData add(@RequestBody SysPermission sysPermission){
        try {
            boolean add = permissionService.add(sysPermission);
            if(add){
                return JsonData.success();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonData.fail(ResultCode.SYSTEM_BUSY.getCode(),ResultCode.SYSTEM_BUSY.getInfo());
    }

    @PostMapping("del.do")
    @ResponseBody
    public JsonData del(Integer id){
        try {
            boolean del = permissionService.del(id);
            if(del){
                return JsonData.success();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonData.fail(ResultCode.SYSTEM_BUSY.getCode(),ResultCode.SYSTEM_BUSY.getInfo());
    }

    @PostMapping("update.do")
    @ResponseBody
    public JsonData update(@RequestBody SysPermission sysPermission){
        try {
            boolean update = permissionService.update(sysPermission);
            if(update){
                return JsonData.success();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonData.fail(ResultCode.SYSTEM_BUSY.getCode(),ResultCode.SYSTEM_BUSY.getInfo());
    }
    @PostMapping("getAllParentPermission.do")
    @ResponseBody
    public JsonData getAllParentPermission(){
        try {
            JSONArray objects = permissionService.permissionTree(null);
            String s = JSONObject.toJSONString(objects);
            List<SysPermission> sysPermissions = JSONObject.parseArray(s, SysPermission.class);
            return JsonData.success(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getInfo(),sysPermissions);
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonData.fail(ResultCode.SYSTEM_BUSY.getCode(),ResultCode.SYSTEM_BUSY.getInfo());
    }
}
